package com.project.api.common.filter;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.project.api.common.ApiResponse;
import com.project.api.sale.common.Weixin;
//import com.project.api.sale.service.UserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import javax.servlet.*;
import javax.servlet.Filter;
import javax.servlet.FilterConfig;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

public class ParentFilter implements Filter {
    private Logger logger = LoggerFactory.getLogger(getClass());
    @Autowired
//    private UserService userService;
    String[] includeUrls = new String[]{};
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        ServletContext context = filterConfig.getServletContext();
        ApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(context);
//        userService =  ctx.getBean(UserService.class);
    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        HttpServletResponse response = (HttpServletResponse) servletResponse;
//        request.setCharacterEncoding("UTF-8");
//        response.setContentType("text/html;charset=utf-8");
//        HttpSession session = request.getSession();
//        Object wxTime1=session.getAttribute("wxTime");//微信access_token过期时间
//        Object token=session.getAttribute("access_token");
//        long time=System.currentTimeMillis();
//        long wxTime=0;
//        if(token==null) {
//            setWxToken(session);
//        }
//        if (wxTime1 != null) {
//            wxTime = (long) wxTime1;
//            if(time-wxTime>=2*60*60*1000){
//                setWxToken(session);
//            }
//        }
//        String openid=(String)session.getAttribute("openid");
//        Object login=session.getAttribute("loginTime");
//        long loginTime=0;
//        if(login!=null){
//            loginTime=(long)login;
//        }
//        if(openid!=null&&time-loginTime>=60*60*1000) {//大于一小时再查一遍
//            int userId = userService.create(openid);
//            if(userId<=0){
//                response.getWriter().write(ApiResponse.Fail("登录失败，请稍后再试!"));
//                return;
//            }
//            session.setAttribute("loginTime",time);
//            session.setAttribute("userId",userId);
//        }
//        String uri = request.getRequestURI();
        filterChain.doFilter(request, response);
    }
    /**
     * @Description: 是否需要过滤
     */
    public boolean isNeedFilter(String uri) {
        for (String includeUrl : includeUrls) {
            if(includeUrl.equals(uri)) {
                return false;
            }
        }
        return true;

    }
    public void setWxToken(HttpSession session){
        String result= Weixin.getAccess_token();
        JSONObject jsonObject= JSON.parseObject(result);
        String access_token=jsonObject.getString("access_token");
        if(access_token!=null){
            session.setAttribute("access_token",access_token);
            session.setAttribute("wxTime",System.currentTimeMillis());
        }else{
            logger.error(result);
        }
    }
    @Override
    public void destroy() {

    }
}
